package com.traffic.between.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.gxhl.api.core.tools.ToolSecuritySHA;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.util.OrderReq;

@SuppressWarnings("deprecation")
public class GxhlServiceImpl {
	private static Logger logger = Logger.getLogger(GxhlServiceImpl.class);
	public static GxhlServiceImpl service = new GxhlServiceImpl();

	private static String HOST_PATH = BetweenProperties.GXHL_URL;
	private static String GXHL_APPKEY = BetweenProperties.GXHL_APPKEY;
	private static String GXHL_SECURITYKEY = BetweenProperties.GXHL_SECURITYKEY;

	@SuppressWarnings("resource")
	public String order(String productId, String phoneNo, String cstmOrderNo) {

		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String resultStr = "";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appkey", GXHL_APPKEY);
		params.put("timeStamp", time);
		params.put("phoneNo", phoneNo);
		params.put("productId", productId);
		params.put("cstmOrderNo", cstmOrderNo);

		String paramsSort = ToolSecuritySHA.dataDecrypt(params);
		String sig = ToolSecuritySHA.shaEncrypt(paramsSort + GXHL_SECURITYKEY);

		OrderReq request = new OrderReq();
		request.setAppkey(GXHL_APPKEY);
		request.setTimeStamp(time);
		request.setSig(sig);
		request.setPhoneNo(phoneNo);
		request.setProductId(productId);
		request.setCstmOrderNo(cstmOrderNo);

		String json = JSONObject.toJSONString(request);
		logger.info("gxhl流量订购请求json: " + json);

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost(HOST_PATH);
		try {
			if (null != json) {
				StringEntity entity = new StringEntity(json.toString(), "utf-8");
				entity.setContentEncoding("utf-8");
				entity.setContentType("application/json");
				method.setEntity(entity);
				method.setHeader("Connection", "close");
			}
			HttpResponse result = httpClient.execute(method);

			if (result.getStatusLine().getStatusCode() == 200)
				try {
					resultStr = EntityUtils.toString(result.getEntity(), "utf-8");
					logger.info("gxhl流量订购请求时的响应数据是: " + resultStr);
				} catch (Exception e) {
					e.printStackTrace();
				}
			else
				resultStr = "{\"code\": \"0001\",\"msg\":\"订购时发生异常,请稍后再试.\",\"data\":{}}";
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (method != null) {
				method.releaseConnection();
				method = null;
			}
		}
		return resultStr;
	}

	@SuppressWarnings("resource")
	public String orderStatus(String cstmOrderNo) {

		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String resultStr = "";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appkey", GXHL_APPKEY);
		params.put("timeStamp", time);
		params.put("cstmOrderNo", cstmOrderNo);

		String paramsSort = ToolSecuritySHA.dataDecrypt(params);
		String sig = ToolSecuritySHA.shaEncrypt(paramsSort + GXHL_SECURITYKEY);

		OrderReq request = new OrderReq();
		request.setAppkey(GXHL_APPKEY);
		request.setTimeStamp(time);
		request.setSig(sig);
		request.setCstmOrderNo(cstmOrderNo);

		String json = JSONObject.toJSONString(request);
		logger.info("gxhl流量订购状态查询请求json: " + json);

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost(HOST_PATH + "/status");
		try {
			if (null != json) {
				StringEntity entity = new StringEntity(json.toString(), "utf-8");
				entity.setContentEncoding("utf-8");
				entity.setContentType("application/json");
				method.setEntity(entity);
				method.setHeader("Connection", "close");
			}
			HttpResponse result = httpClient.execute(method);

			if (result.getStatusLine().getStatusCode() == 200)
				try {
					resultStr = EntityUtils.toString(result.getEntity(), "utf-8");
					logger.info("gxhl流量订购状态查询请求时的响应数据是: " + resultStr);
				} catch (Exception e) {
					e.printStackTrace();
				}
			else
				resultStr = "{\"code\": \"0001\",\"msg\":\"订购状态时发生异常,请稍后再试.\",\"data\":{}}";
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (method != null) {
				method.releaseConnection();
				method = null;
			}
		}
		return resultStr;
	}
}